//
//  EditReviewsViewController.m
//  CenturyInterconnection
//
//  Created by 郭先生 on 16/4/19.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "EditReviewsViewController.h"
#import "VPImageCropperViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "ImageCache.h"
#import "SAMTextView.h"


#define ORIGINAL_MAX_WIDTH 640.0f

@interface EditReviewsViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, VPImageCropperDelegate,UITextFieldDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIButton*photoButton;
@property(nonatomic,strong)SAMTextView *commentsTextField;

@property(nonatomic,strong)UIButton *StarBtn1;
@property(nonatomic,strong)UIButton *StarBtn2;
@property(nonatomic,strong)UIButton *StarBtn3;
@property(nonatomic,strong)UIButton *StarBtn4;
@property(nonatomic,strong)UIButton *StarBtn5;

@property(nonatomic,strong)NSNumber *starStr;
@property(nonatomic,assign)BOOL canSave;
@end

@implementation EditReviewsViewController

#pragma mark =============== 懒加载 ===============
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource=self;
//        _tableView.bounces=NO;
        _tableView.backgroundColor=[UIColor clearColor];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self makeNaviButton];
    self.navigationItem.title = @"WRITE A REVIEW";
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:[NSString stringWithFormat:@"EditReviewsPage >>>>> %ld",[self.productID integerValue]]];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick beginLogPageView:[NSString stringWithFormat:@"EditReviewsPage >>>>>> %ld",[self.productID integerValue]]];
}

-(void)makeNaviButton{
    UIButton *forkButton=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(320), 0, ACTUAL_WIDTH(30), ACTUAL_HEIGHT(30))];
    [forkButton setImage:[UIImage imageNamed:@"nav_close"] forState:0];
    
    //    forkButton.backgroundColor=[UIColor yellowColor];
    [forkButton addTarget:self action:@selector(comeBackToRoot) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*item1 = [[UIBarButtonItem alloc]initWithCustomView:forkButton];
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [leftButton setBackgroundImage:[UIImage imageNamed:@"nav_back"] forState:0];
    
    leftButton.frame=CGRectMake(0, 0, ACTUAL_WIDTH(23), ACTUAL_HEIGHT(23));
    
    [leftButton addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithCustomView:leftButton];
    
    
    self.navigationItem.leftBarButtonItem=item;
    self.navigationItem.rightBarButtonItem=item1;
}

#pragma mark 返回事件
-(void)comeBack{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)comeBackToRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0) {
        return ACTUAL_HEIGHT(80);
    }else if (indexPath.row==1){
        return ACTUAL_HEIGHT(250);
    }else{
        return 100;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.row==0) {
        UIView *bgView0 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ACTUAL_HEIGHT(80))];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(20), ACTUAL_WIDTH(10), ACTUAL_WIDTH(100), ACTUAL_HEIGHT(25))
                          ];
        label.text=@"RATEING:";
        [bgView0 addSubview:label];
       
        //星星
        _StarBtn1=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(60), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
        //    StarBtn1.backgroundColor=[UIColor redColor];
        [_StarBtn1 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
        [_StarBtn1 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
        [_StarBtn1 addTarget:self action:@selector(showStarbtn1) forControlEvents:UIControlEventTouchUpInside];
        [bgView0 addSubview:_StarBtn1];
        
        _StarBtn2=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(120), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
        //    StarBtn2.backgroundColor=[UIColor redColor];
        [_StarBtn2 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
        [_StarBtn2 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
        [_StarBtn2 addTarget:self action:@selector(showStarbtn2) forControlEvents:UIControlEventTouchUpInside];
        [bgView0 addSubview:_StarBtn2];
        
        _StarBtn3=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(180), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
        //    StarBtn3.backgroundColor=[UIColor redColor];
        [_StarBtn3 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
        [_StarBtn3 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
        [_StarBtn3 addTarget:self action:@selector(showStarbtn3) forControlEvents:UIControlEventTouchUpInside];
        [bgView0 addSubview:_StarBtn3];
        
        _StarBtn4=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(240), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
        //    StarBtn4.backgroundColor=[UIColor redColor];
        [_StarBtn4 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
        [_StarBtn4 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
        [_StarBtn4 addTarget:self action:@selector(showStarbtn4) forControlEvents:UIControlEventTouchUpInside];
        [bgView0 addSubview:_StarBtn4];
        
        _StarBtn5=[[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(300), ACTUAL_HEIGHT(40), ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20))];
        //    StarBtn5.backgroundColor=[UIColor redColor];
        [_StarBtn5 setImage:[UIImage imageNamed:@"img_star"] forState:UIControlStateSelected];
        [_StarBtn5 setImage:[UIImage imageNamed:@"img_star_grey"] forState:0];
        [_StarBtn5 addTarget:self action:@selector(showStarbtn5) forControlEvents:UIControlEventTouchUpInside];
        [bgView0 addSubview:_StarBtn5];
        
        [cell addSubview:bgView0];
    }
    if (indexPath.row==1) {
        UIView *bgView1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ACTUAL_HEIGHT(250))];
        
        //相机
//        UIButton* photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        
//        [photoButton setImage:[UIImage imageNamed:@"img_camera"] forState:UIControlStateNormal];
//        photoButton.frame = CGRectMake(ACTUAL_WIDTH(320), ACTUAL_HEIGHT(200), ACTUAL_WIDTH(40),ACTUAL_HEIGHT(40));
//        //无法完美添加照片 先锁掉
////        [photoButton addTarget:self action:@selector(touchPhotoButton) forControlEvents:UIControlEventTouchDown];
//        self.photoButton=photoButton;
//        [bgView1 addSubview:photoButton];
        
        _commentsTextField=[[SAMTextView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ACTUAL_HEIGHT(190))];
//        _commentsTextField.backgroundColor=[UIColor yellowColor];
        [bgView1 addSubview:_commentsTextField];
        
        [cell addSubview:bgView1];
    }
    if (indexPath.row==2) {
        UIView *bgView2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, ACTUAL_HEIGHT(100))];
        bgView2.backgroundColor=[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
        
        UIButton * button = [[UIButton alloc]initWithFrame:CGRectMake(ACTUAL_WIDTH(20), ACTUAL_HEIGHT(20), self.view.bounds.size.width-40, ACTUAL_HEIGHT(50))];
        [button setTitle:@"SUBMIT" forState:0];
        button.backgroundColor = [UIColor colorWithRed:0.12 green:0.12 blue:0.12 alpha:1];
        [button addTarget:self action:@selector(sendTheComments:) forControlEvents:UIControlEventTouchUpInside];
        [bgView2 addSubview:button];
        [cell addSubview:bgView2];
    }
    return cell;
}

//#pragma mark =============== 调用相机 ===============
//-(void)touchPhotoButton:(id)sender{
//    
//    UIActionSheet * myActionSheet;
//    //判断是否支持相机
//    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
//
//    myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", @"拍照", nil];
//    }else{
//            myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册", @"拍照", nil];
//    }
//    [myActionSheet showInView:self.view];
//}
//-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
//    NSUInteger sourceType = 0;
//    switch (buttonIndex) {
//        case 0:
//            //相机
//            sourceType = UIImagePickerControllerSourceTypeCamera;
//            break;
//        case 1:
//            //相册
//            sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//            break;
//        case 2:
//            return;
//    
//    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
//            
//        imagePickerController.delegate = self;
//        imagePickerController.allowsEditing = YES;
//            
//        imagePickerController.sourceType = sourceType;
//            [self presentViewController:imagePickerController animated:YES completion:^{
//        }];
//    }
//}
//
//- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//     [picker dismissViewControllerAnimated:YES completion:^{}];
//    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//    
//     // 保存图片至本地，方法见下文
////      [self saveImage:image withName:@"currentImage.png"];
//}

#pragma mark  ----  照相机
//开始

- (void)touchPhotoButton {
    UIActionSheet *choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"拍照", @"从相册中选取", @"系统头像",nil];
    [choiceSheet showInView:self.view];
}

#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
//    self.photoButton.imageView.image = editedImage;
    [self.photoButton setBackgroundImage:editedImage forState:UIControlStateNormal];
    __weak EditReviewsViewController*weakSelf=self;
    
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
        // TO DO
        NSString *str = [ImageCache headImagePath:editedImage];
        NSData *fileData = [NSData dataWithContentsOfFile:str];
        
        
        
        //        NSString*urlStr=@"http://www.vipxox.cn/module/appapi/upload_file.php";
//        NSString*urlStr=[NSString stringWithFormat:@"%@/module/appapi/upload_file.php",HTTP_ADDRESS];
//        NSDictionary*params=@{@"uid":[UserSession instance].uid};
//        HttpManager *manager=[[HttpManager alloc]init];
//        [manager postDataUpDataPhotoWithUrl:urlStr parameters:params photo:fileData compliation:^(id data, NSError *error) {
//            NSLog(@"%@",data);
//            if ([data[@"errorCode"] isEqualToString:@"0"]) {
//                [UserSession instance].logo=data[@"imgurl"];
//                [_photoView sd_setImageWithURL:[NSURL URLWithString:data[@"imgurl"]] placeholderImage:nil];
//                //                HTTP_ADDRESS
//            }else{
//                [JRToast showWithText:@"5月震撼上线"];
//                
//            }
//        }];
    }];
}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        // 拍照
        if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]) {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            if ([self isFrontCameraAvailable]) {
                controller.cameraDevice = UIImagePickerControllerCameraDeviceFront;
            }
            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
            controller.mediaTypes = mediaTypes;
            controller.delegate = self;
            [self presentViewController:controller
                               animated:YES
                             completion:^(void){
                                 NSLog(@"Picker View Controller is presented");
                             }];
        }
        
    } else if (buttonIndex == 1) {
        // 从相册中选取
        if ([self isPhotoLibraryAvailable]) {
            UIImagePickerController *controller = [[UIImagePickerController alloc] init];
            controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
            [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
            controller.mediaTypes = mediaTypes;
            controller.delegate = self;
            [self presentViewController:controller
                               animated:YES
                             completion:^(void){
                                 NSLog(@"Picker View Controller is presented");
                             }];
        }
    }else if (buttonIndex==2){
        
//        SystemImageViewController*vc=[[SystemImageViewController alloc]init];
//        vc.delegate=self;
//        [self.navigationController pushViewController:vc animated:YES];
    }
}
//-(void)delegateForBack4:(NSString *)str4{
//    [UserSession instance].logo=str4;
//    [_photoView sd_setImageWithURL:[NSURL URLWithString:[UserSession instance].logo] placeholderImage:nil];
//}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        portraitImg = [self imageByScalingToMaxSize:portraitImg];
        // 裁剪
        VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];  //倍数缩放
        imgEditorVC.delegate = self;
        [self presentViewController:imgEditorVC animated:YES completion:^{
            // TO DO
        }];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^(){
    }];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
}

#pragma mark camera utility
- (BOOL) isCameraAvailable{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isRearCameraAvailable{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

- (BOOL) isFrontCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

- (BOOL) doesCameraSupportTakingPhotos {
    return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isPhotoLibraryAvailable{
    return [UIImagePickerController isSourceTypeAvailable:
            UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickVideosFromPhotoLibrary{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickPhotosFromPhotoLibrary{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
    __block BOOL result = NO;
    if ([paramMediaType length] == 0) {
        return NO;
    }
    NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
    [availableMediaTypes enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *mediaType = (NSString *)obj;
        if ([mediaType isEqualToString:paramMediaType]){
            result = YES;
            *stop= YES;
        }
    }];
    return result;
}

#pragma mark image scale utility
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {
    if (sourceImage.size.width < ORIGINAL_MAX_WIDTH) return sourceImage;
    CGFloat btWidth = 0.0f;
    CGFloat btHeight = 0.0f;
    if (sourceImage.size.width > sourceImage.size.height) {
        btHeight = ORIGINAL_MAX_WIDTH;
        btWidth = sourceImage.size.width * (ORIGINAL_MAX_WIDTH / sourceImage.size.height);
    } else {
        btWidth = ORIGINAL_MAX_WIDTH;
        btHeight = sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
    }
    CGSize targetSize = CGSizeMake(btWidth, btHeight);
    return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}

- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
    }
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil) NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

#pragma mark 评论星星的点击事件
-(void)showStarbtn1{
    _StarBtn1.selected=YES;
    _StarBtn2.selected=NO;
    _StarBtn3.selected=NO;
    _StarBtn4.selected=NO;
    _StarBtn5.selected=NO;
    
    self.starStr=@(1);
}

-(void)showStarbtn2{
    _StarBtn1.selected=YES;
    _StarBtn2.selected=YES;
    _StarBtn3.selected=NO;
    _StarBtn4.selected=NO;
    _StarBtn5.selected=NO;
    
    self.starStr=@(2);
}

-(void)showStarbtn3{
    _StarBtn1.selected=YES;
    _StarBtn2.selected=YES;
    _StarBtn3.selected=YES;
    _StarBtn4.selected=NO;
    _StarBtn5.selected=NO;
    
    self.starStr=@(3);
}

-(void)showStarbtn4{
    _StarBtn1.selected=YES;
    _StarBtn2.selected=YES;
    _StarBtn3.selected=YES;
    _StarBtn4.selected=YES;
    _StarBtn5.selected=NO;
    
    self.starStr=@(4);
}

-(void)showStarbtn5{
    _StarBtn1.selected=YES;
    _StarBtn2.selected=YES;
    _StarBtn3.selected=YES;
    _StarBtn4.selected=YES;
    _StarBtn5.selected=YES;
    
    self.starStr=@(5);
}

#pragma mark 滚动tableview的时候收起键盘

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
}

#pragma mark SUMBIT按钮的点击事件
-(void)sendTheComments:(UIButton*)sender{
    //    int countryID=[self.countryStr intValue];
    if (sender.selected==YES) {
        return;
    }else{
        sender.selected=YES;
        [self performSelector:@selector(timeEnough:) withObject:nil afterDelay:2.0];
        NSString*str=[self judgeCansave];
        
        if (_canSave) {
            
            // http://viprunningonline.com/api/index.php?  main_page=address_book_process
            
            NSString*aaa=@"main_page=product_reviews_write";
            NSString*urlStr=[NSString stringWithFormat:@"%@%@",HTTP_ADDRESS,aaa];
            
            NSDictionary*params=@{@"products_id":self.productID,
                                  @"review_text":_commentsTextField.text,
                                  @"rating":self.starStr};
            HttpManager*manager=[[HttpManager alloc]init];
            
            [manager postDataFromNetworkWithUrl:urlStr parameters:params compliation:^(id data, NSError *error) {
                
                NSString*number=[NSString stringWithFormat:@"%@",data[@"status"]];
                
                if ([number isEqualToString:@"1"]) {
                    [JRToast showWithText:@"Comment on success！"];
                    [self comeBack];
                }else{
                    [JRToast showWithText:data[@"status_message"]];
                }
            }];
            
        }else{
            [JRToast showWithText:str duration:2.0];
        }
    }
}

-(void)timeEnough:(NSTimer*)timer{
    UIButton *btn=(UIButton*)[self.view viewWithTag:333];
    btn.selected=NO;
    [timer invalidate];
    timer=nil;
}

#pragma mark 未填写警告

-(NSString*)judgeCansave{
    _canSave=YES;
    
    if (_commentsTextField.text.length==0) {
        _canSave=NO;
        return @"Please fill in the comments！";
    }
    
    if (self.starStr==0) {
        _canSave=NO;
        return @"Please score first！";
    }
    return @"666";
}




@end
